package com.wzh.bargain.controller;

import java.util.Arrays;
import java.util.Map;

import com.wzh.bargain.vo.OrderVo;
import com.wzh.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.bargain.entity.OrderBargainEntity;
import com.wzh.bargain.service.OrderBargainService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-03-13 23:09:33
 */
@RestController
@RequestMapping("bargain/orderbargain")
public class OrderBargainController {
    @Autowired
    private OrderBargainService orderBargainService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null){
                params.put("userId",claims.getId());
                PageUtils page = orderBargainService.queryPage(params);
                return R.ok().put("page", page);
            }
            return R.error("用户未登录");
        }
        return R.error("用户未登录");
    }

    /**
     * 初始化砍价订单
     */
    @RequestMapping("/addOrder/{url}/{bargainId}/{goodsId}")
    public R addOrder(@PathVariable("url")String url, @PathVariable("bargainId")Long bargainId
            , @PathVariable("goodsId")Long goodsId, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null){
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null&&"admin".equals(claims.get("roles"))){
                String path = (String) redisTemplate.opsForValue().get(claims.getId() + "And" + bargainId);
                if (path!=null&&path.equals(url)){
                    OrderVo orderVo = new OrderVo();
                    orderVo.setUserId(Long.valueOf(claims.getId()));
                    orderVo.setActivityId(bargainId);
                    orderVo.setGoodsId(goodsId);
                    return orderBargainService.addOrder(orderVo);
                }
                return R.error("非法访问");
            }
            return R.error("用户未登录");
        }
        return R.error("用户未登录");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OrderBargainEntity orderBargain = orderBargainService.getById(id);

        return R.ok().put("orderBargain", orderBargain);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderBargainEntity orderBargain){
		orderBargainService.save(orderBargain);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderBargainEntity orderBargain){
		orderBargainService.updateById(orderBargain);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderBargainService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
